<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>列表数据展示案例</title>
    <script src="./js/vue2.js"></script>
    <link rel="stylesheet" href="./css/animate.css">
    <link rel="stylesheet" href="./css/弹框 .css">
    <style>
        #app {
            width: 600px;
            margin: 10px auto;
        }

        .tb {
            border-collapse: collapse;
            width: 100%;
        }

        .tb th {
            background-color: #0094ff;
            color: white;
        }

        .tb td,
        .tb th {
            padding: 5px;
            border: 1px solid black;
            text-align: center;
        }

        .add {
            padding: 5px;
            border: 1px solid black;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="add">
            编号:
            <input type="text" v-myfocus v-color='"skyblue"' v-model="Count.id"> 品牌名称:
            <input type="text" v-color='"purple"' v-model="Count.name" @keydown.enter="add">
            <input type="button" value="添加" @click="add">
        </div>
        <div class="add">
            品牌名称:
            <input type="text" placeholder="请输入搜索条件" v-model="searchvalue">
        </div>
        <div>
            <table class="tb">
                <tbody>
                    <tr>
                        <th>编号</th>
                        <th>品牌名称</th>
                        <th>创立时间</th>
                        <th>操作</th>
                    </tr>
                    <tr v-for="(value,index) in listdata" :key="value.id">
                        <td>{{value.id}}</td>
                        <td>{{value.name}}</td>
                        <td>{{value.time | mytime('/')}}</td>
                        <td>
                            <a href="#" @click.prevent.stop="deleteData(index)">删除</a>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" v-show="listdata.length==0">啥都咩得咯！</td>
                    </tr>
                </tbody>
            </table>
        </div>
        <!-- 弹框 -->
        <transition name="" mode=""
        enter-active-class='animated zoomIn'
        leave-active-class='animated zoomOut'
        > 
            <div id="popBox" v-show="isshow">
                <div class="close">
                    <a href="javascript:void(0)" @click="close">温馨提示</a>
                </div>
                <div id="tip">一个字，就是干！</div>
                <input type="button" value="确定" id="anniu1" @click="queding">
                <input type="button" value="取消" id="anniu2" @click="quxiao">
            </div>
        </transition>
    </div>
</body>
<script>
    // 自定义输入框的颜色的指令
    Vue.directive('color', {
        inserted(el, binding) {
            el.style.color = binding.value;
        }
    })
    // 自定义指令创建聚焦
    Vue.directive('myfocus', {
        inserted(el) {
            el.focus();
        }
    })
    // 创建时间过滤器
    Vue.filter('mytime', function (time, sc) {
        var date = new Date(time);
        var year = date.getFullYear();
        /* 在日期格式中，月份是从0开始的，因此要加0
         * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
         * */
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        // 拼接
        return year + sc + month + sc + day + " " + hours + ":" + minutes + ":" + seconds;
    })
    var ms = new Vue({
        // 定义模块
        el: '#app',
        data: {
            isshow: false,
            searchvalue: '',
            deleteRow: '',
            list: [
                { id: 1, name: '流浪地球', time: '2019/06/25 09:38:12' },
                { id: 2, name: '钢铁侠', time: '2019/06/25 09:38:12' },
                { id: 3, name: '复仇者联盟', time: '2019/06/25 09:38:12' },
                { id: 4, name: '前任三-再见前任', time: '2019/06/25 09:38:12' },
                { id: 5, name: '千与千寻', time: '2019/06/25 09:38:12' },
                { id: 6, name: '美国队长', time: '2019/06/25 09:38:12' },
                { id: 7, name: '紧急救援', time: '2019/06/25 09:38:12' }
            ],
            // colorvalue:'red',
            Count: { id: "",name: "",time: Date()}
        },
        methods: {
            add() {
                // 修改当前的时间
                this.Count.time = new Date();
                // 把新的对象(添加进数组
                this.list.push({ ...this.Count });
                // 清空数据
                this.Count = { id: "",name: "" };
                // console.log(new Date());
                // console.log(Date());
            },
            deleteData(id) {
                this.isshow = true;
                this.deleteRow = id;
            },
            queding() {
                console.log(this.deleteRow)
                this.list.splice(this.deleteRow, 1);
                this.isshow = false;
            },
            quxiao() {
                this.isshow = false;
            },
            close(){
               this.isshow= false; 
            }
        },
        computed: {
            listdata() {
                return this.list.filter(s => s.name.indexOf(this.searchvalue) != -1)
            }
        }
    })
</script>
</html>